package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhang.mapper.IOutputCarMapper;
import com.zhang.pojo.OutputCarBean;
import com.zhang.pojo.OutputInfoBean;
import com.zhang.pojo.ProductBean;
import com.zhang.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OutputCarServiceImpl implements IOutputCarService {
    @Autowired
    private IOutputCarMapper outputCarMapper;
    @Autowired
    private ICustomService customService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IOutputInfoService outputInfoService;

    @Override
    public List<OutputCarBean> findAll(int customId, int userId) {
        QueryWrapper<OutputCarBean> wrapper = new QueryWrapper<>();
        wrapper.eq("fk_customId",customId);
        wrapper.eq("fk_userId",userId);
        return outputCarMapper.selectList(wrapper);
    }

    @Override
    public void add(ProductBean productBean,int customId,int userId) {
        OutputCarBean outputCarBean = new OutputCarBean();
        outputCarBean.setNumber(productBean.getNumber());
        outputCarBean.setName(productBean.getName());
        outputCarBean.setMill(productBean.getMill());
        outputCarBean.setNum(productBean.getCount());
        outputCarBean.setSale(productBean.getSale());
        outputCarBean.setTypeId(productBean.getTypeId());
        outputCarBean.setCustomId(customId);
        outputCarBean.setUserId(userId);
        outputCarMapper.insert(outputCarBean);
    }

    @Override
    public void del(int id) {
        outputCarMapper.deleteById(id);
    }

    @Override
    public void delAll(int customId, int userId,int total,String address) {
        String realName = userService.findById(userId).getReal();
        String customName = customService.findById(customId).getName();
        List<OutputCarBean> outputCarBeans = this.findAll(customId, userId);

        outputInfoService.add(new OutputInfoBean(customName,realName,total,address),outputCarBeans);
        productService.output(outputCarBeans);

        UpdateWrapper<OutputCarBean> wrapper= new UpdateWrapper<>();
        wrapper.eq("fk_customId",customId);
        wrapper.eq("fk_userId",userId);
        outputCarMapper.delete(wrapper);
    }

    @Override
    public void update(int carId,int count) {
        outputCarMapper.update(carId, count);
    }
}
